package com.backsidecode.controller;

import cn.hutool.core.bean.BeanUtil;
import com.backsidecode.common.BaseContext;
import com.backsidecode.common.Result;
import com.backsidecode.pojo.dto.UserLoginDto;
import com.backsidecode.pojo.entity.CourseVariable;
import com.backsidecode.pojo.entity.Courses;
import com.backsidecode.pojo.entity.User;
import com.backsidecode.pojo.vo.UserVo;
import com.backsidecode.service.CourseVariableService;
import com.backsidecode.service.CoursesService;
import com.backsidecode.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "用户信息管理")
@CrossOrigin
@RequestMapping("/User")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    CoursesService coursesService;

    @Autowired
    CourseVariableService courseVariableService;

    @ApiOperation(value = "用户登录接口", notes = "需要转递学号studentNumber和密码password")
    @PostMapping("/login")
    public Result<String> login(@ApiParam("用户登录Dto") @RequestBody UserLoginDto userLoginDto) {
        //获取当前学生的学号
        String userName = userLoginDto.getUserName();
        //获取经过md5加密后的密码
        String md5Password = DigestUtils.md5DigestAsHex(userLoginDto.getPassword().getBytes());

        log.info("登录账号:{},密码:{}", userName, md5Password);

        String token = userService.login(userName, md5Password);

        return Result.success(token);

    }


    @ApiOperation(value = "用户选课接口", notes = "传递一个课程的id")
    @PostMapping("/addCourse")
    public Result<String> addCourse(@RequestBody Courses course) {
        //获取当前登录线程的用户id
        Long currentId = BaseContext.getCurrentId();

        log.info("当前用户id:{},选择课程id：{}", currentId, course.getId());

        //将数据插入到选课表中
        CourseVariable courseVariable = new CourseVariable();
        courseVariable.setUuid(currentId);
        courseVariable.setCuid(course.getId());

        courseVariableService.save(courseVariable);

        return Result.success("选课成功");
    }


    @ApiOperation(value = "查询已选课程信息")
    @GetMapping("/selectCourseVariable")
    public Result<List<Courses>> selectCourseVariable() {

        //获取当前登录线程的用户id
        Long currentId = BaseContext.getCurrentId();

        log.info("当前用户id:{}", currentId);

        List<Courses> list = courseVariableService.getCourseList(currentId);

        return Result.success(list);
    }

    @ApiOperation(value = "删除已选课程信息")
    @PostMapping("/deleteCourseVariable")
    public Result<String> deleteCourseVariable(@RequestBody CourseVariable courseVariable) {
        //获取当前登录线程的用户id
        Long currentId = BaseContext.getCurrentId();
        log.info("当前用户id:{},删除课程id：{}", currentId, courseVariable.getId());

        LambdaQueryWrapper<CourseVariable> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CourseVariable::getUuid, currentId);
        lambdaQueryWrapper.eq(CourseVariable::getCuid, courseVariable.getId());

        courseVariableService.remove(lambdaQueryWrapper);
        return Result.success("删除成功");
    }


    @ApiOperation(value = "查询个人信息")
    @GetMapping
    public Result<UserVo> getUserInfo() {
        Long currentId = BaseContext.getCurrentId();
        User user = userService.getById(currentId);
        if (user.getStatus().equals("1")) {
            user.setStatus("管理员");
        } else {
            user.setStatus("学生");
        }
        UserVo userVo = new UserVo();
        //数据拷贝
        BeanUtil.copyProperties(user, userVo);
        return Result.success(userVo);
    }

}
